<script setup lang="ts" name="SystemSensitiveWordCheckForm">
import { storeToRefs } from 'pinia'
import { useAppStoreWithOut } from '@/store/modules/app'
import { SensitiveWordApi, SensitiveWordCheckReqType } from '@/api/system/sensitiveWord'
const appStore = useAppStoreWithOut()
const { systemConfig } = storeToRefs(appStore)

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const visible = ref(false) // 弹窗的是否展示
const title = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData = ref({
  text: '',
  tags: []
})
const formRules = reactive({
  text: [{ required: true, message: t('sys.sensitiveWord.rules', 'text'), trigger: 'blur' }],
  tags: [{ required: true, message: t('sys.sensitiveWord.rules', 'tags'), trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
const tagList = ref([]) // 标签数组

/** 打开弹窗 */
const open = async () => {
  visible.value = true
  title.value = t('sys.sensitiveWord', 'check')
  resetForm()
  // 获得 Tag 标签列表
  tagList.value = await SensitiveWordApi.tag()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const form = formData.value as SensitiveWordCheckReqType
    const data = await SensitiveWordApi.validate(form)
    if (data.length === 0) {
      message.success(t('sys.sensitiveWord.rules', 'noSensitive'))
      return
    }
    message.warning(t('sys.sensitiveWord.rules', 'hasSensitive', [data.join(', ')]))
    visible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    text: '',
    tags: []
  }
  formRef.value?.resetFields()
}
</script>

<template>
  <Dialog v-model="visible" :title="title" width="600px">
    <el-form ref="formRef" v-loading="formLoading && systemConfig.loadingType === 'module'" :model="formData" :rules="formRules" label-width="80px">
      <el-form-item :label="t('sys.sensitiveWord', 'text')" prop="text">
        <el-input v-model="formData.text" clearable :placeholder="t('sys.sensitiveWord.rules', 'text')" type="textarea" />
      </el-form-item>
      <ElFormItem :label="t('sys.sensitiveWord', 'tags')" prop="tags">
        <el-select v-model="formData.tags" clearable allow-create filterable multiple :placeholder="t('sys.sensitiveWord.rules', 'tags')">
          <el-option v-for="tag in tagList" :key="tag" :label="tag" :value="tag" />
        </el-select>
      </ElFormItem>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">{{ t('action', 'cancel') }}</el-button>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">{{ t('action', 'confirm') }}</el-button>
    </template>
  </Dialog>
</template>

<style scoped lang="scss">
.el-form{
  margin-right: -6px;
}
</style>
